package com.xbongbong.saas.enums.dictionary;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import lombok.Getter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 数据字典-采购合同状态
 *
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/26 10:19
 * @since v1.0
 */
@Getter
public enum PurchaseStatusEnum {

    // 采购合同状态
    ORDER_GENERATION("1", "enum.purchaseStatus.name.order_generation", "orderGeneration"),


    PAID("2", "enum.purchaseStatus.name.paid", "paid"),


    SHIPPED("3", "enum.purchaseStatus.name.shipped", "shipped"),


    ARRIVED("4", "enum.purchaseStatus.name.arrived", "arrived"),


    INSTOCK("5", "enum.purchaseStatus.name.instock", "instock"),


    RETURN_GOODS("6", "enum.purchaseStatus.name.return_goods", "returnGoods"),


    PARTIAL_RETURN("7", "enum.purchaseStatus.name.partial_return", "partialReturn"),


    CANCEL("8", "enum.purchaseStatus.name.cancel", "cancel"),


    UNKNOWN("404", "enum.purchaseStatus.name.unknown", "unknown"),
    ;

    private String code;
    private String name;
    private String attr;

    PurchaseStatusEnum(String code, String name, String attr) {
        this.code = code;
        this.name = name;
        this.attr = attr;
    }

    /**
     * 获取全部枚举
     */
    public static List<PurchaseStatusEnum> getAllEnum() {
        List<PurchaseStatusEnum> list = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (PurchaseStatusEnum cache : values()) {
            if (cache == UNKNOWN) {
                continue;
            }
            list.add(cache);
        }
        return list;
    }

    /**
     * 获取出库类型code和name的关系
     *
     * @author lcx
     * @since v1.0
     */
    public static Map<String, String> getPurchaseStatusMap() {
        Map<String, String> typeMap = new LinkedHashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (PurchaseStatusEnum purchaseStatusEnum : getAllEnum()) {
            typeMap.put(purchaseStatusEnum.getCode(), purchaseStatusEnum.getName());
        }
        return typeMap;
    }

    public static List<JSONObject> getItemPoJoList(String status) {
        List<JSONObject> itemPoJoList = new ArrayList<>();
        List<PurchaseStatusEnum> allEnum = getAllEnum();
        allEnum.forEach(item -> {
            JSONObject itemObj = new JSONObject();
            itemObj.put("text", item.getName());
            itemObj.put("value", item.getCode());
            if (Objects.equals(Objects.toString(item.getCode()), status)) {
                itemObj.put("checked", true);
            }
            itemPoJoList.add(itemObj);
        });
        return itemPoJoList;
    }

    public static String getName(String code){
        String name = null;
        for (PurchaseStatusEnum purchaseStatusEnum : getAllEnum()) {
            if (Objects.equals(code,purchaseStatusEnum.getCode())){
                name = purchaseStatusEnum.getName();
            }
        }
        return name;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }
}
